<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col v-if="!this.$route.query.id" :span="12">
          <el-form-item :label="$t('项目名称')" prop="dealId">
            <MainBodySelect
              v-model="form.dealId"
              parameters="deal"
              @change="dealIdChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('款项名称')" prop="collectionName">
            <el-input v-model="form.collectionName" maxlength="200" show-word-limit :placeholder="$t('款项名称')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('关联合同')" prop="joinContract">
            <el-select v-model="form.joinContract" filterable clearable :placeholder="$t('关联合同')">
              <el-option
                v-for="(item, index) in contractOptions"
                :key="index"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('收款时间')" prop="collectionTime">
            <DatePicker v-model="form.collectionTime" :placeholder="$t('收款时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('收款金额') + $t('金额单位')" prop="collectionMoney">
            <money v-model="form.collectionMoney" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('收款状态')" prop="collectionState">
            <el-radio-group v-model="form.collectionState">
              <el-radio
                v-for="dict in collectionStateOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否开具发票')">
            <el-radio-group v-model="form.invoice">
              <el-radio
                v-for="dict in invoiceOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" maxlength="2000" show-word-limit :placeholder="$t('备注')" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" has-permi="deal:collection" data-type="deal_collection" :business-id="autoBusinessId($route.query.id, form)" :btn-show="collectionStateBtn" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="deal:collection" :save-show="collectionStateBtn" />
    </div>
  </div>
</template>

<script>
    import { saveDealCollection, getDealCollection } from '@/api/deal/collection/dealCollection'
    // import { selectContractByDealIdW } from '@/api/deal/contract/dealContract'
    import MainBodySelect from '@/components/MainBodySelect'
    import { listDataContract } from '@/api/oa/contract/oaContract'
    export default {
      components: {
        MainBodySelect
      },
        data() {
            return {
              // 是否开具发票字典
              invoiceOptions: [], // 收款状态字典
              collectionStateOptions: [], // 表单参数
              form: {
                fileList: []
              },
              // 遮罩层
              loading: false,
              // 合同字典
              contractOptions: [],
              // 表单校验
              rules: {
                collectionName: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ], collectionTime: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ], collectionMoney: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ], collectionState: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ], dealId: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ]
              },
              collectionStateBtn: false
            }
        },
        created() {
          this.getDicts('invoice_yn').then(response => {
              this.invoiceOptions = response.data
          })
          this.getDicts('collection').then(response => {
              this.collectionStateOptions = response.data
          })
          /*
          selectContractByDealIdW(this.$route.query.id).then(response => {
            this.contractOptions = response.data
          }) */
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                this.collectionStateBtn = false
                if (data != null && data.id !== undefined) {
                    getDealCollection(data.id).then(response => {
                        this.form = response.data
                        this.getContract(this.form.dealId)
                        if (this.form.collectionState !== 'YSK') {
                          this.collectionStateBtn = true
                        }
                        this.loading = false
                    })
                } else {
                    this.getContract(this.$route.query.id)
                    this.collectionStateBtn = true
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                dealId: this.$route.query.id,
                collectionName: undefined,
                joinContract: undefined,
                collectionTime: undefined,
                collectionMoney: undefined,
                invoice: 'N',
                collectionState: 'WSK',
                fileList: [],
                remark: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealCollection(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                                this.loading = false
                            }
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
          getContract(dealId) {
            // 合同
            const params = {
              dealId: dealId
            }
            listDataContract(params).then(response => {
              this.contractOptions = response.data
            })
          },
          dealIdChange(val) {
              this.getContract(val)
          }
        }

    }
</script>
